<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\App;
use think\exception\ValidateException;
use think\facade\View;
use think\response\Json;

class Version extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Version';

    //验证器
    protected $validate = 'Version';


    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'type', 'title' => '类型', 'modify' => 'add', 'format' => 'select','value' => self::getType(), 'templet' => '<span>{{d.type==1?"Android":"iOS"}}</span>'])
            ->addRow(['field' => 'version_no', 'title' => '版本号', 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'content', 'title' => '更新内容', 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'down_url', 'title' => '下载地址', 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'create_time', 'title' => '添加时间'])
            ->addRow(['field' => 'is_new', 'title' => '是否最新版本', 'templet' => '<span>{{d.is_new==1?"是":"否"}}</span>'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作'])
            ->clearTable();

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addTopButtons(['add'])
            ->addRightButtons(['edit', 'delete'])
            ->setSearch($this->setSearch())
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['content', '更新内容', 'input', '', '', '', 'like']);
    }


    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }

    public static function getType()
    {
        $type = [
            [
                'id' => 1,
                'title' => 'Android'
            ],[
                'id' => 2,
                'title' => 'iOS'
            ]
        ];

        return $type;
    }

}
